new Vue({
  el: '#app',
  data() {
    return {
      form: {
        Approval_Info_Name: '',
        Function_Approval_SerialNumber: '',
        Approval_Auto_Pass: '',
      },
      rules: {

      },
      function_approval_config: [],
      formData: null,
      loading: false
    }
  },
  mounted() {
    const form = sessionStorage.getItem("form")
    if (form) {
      this.form = JSON.parse(form);
    }
    const stringify = sessionStorage.getItem("approvalJSON")
    if (stringify) {
      this.formData = JSON.parse(stringify);
    }
    this.getData();
  },
  methods: {
    getData() {
      const _this = this;
      $.ajax({
        url: DomainName + '/getInquiryAction?token=' + $.cookie('token'),
        type: 'POST',
        data: {
          Data: 'function_approval_config',
        },
        success: function (res) {
          _this.function_approval_config = res;
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    config() {
      sessionStorage.setItem("form",JSON.stringify(this.form))
      window.location.href = './dataFlow.html'
    },
    check() {
      if (this.formData) {
        this.formData.workFlowDef.name = this.form.Approval_Info_Name;
        this.formData.workFlowDef.type = this.form.Function_Approval_SerialNumber;
        this.formData.approval_auto_pass = this.form.Approval_Auto_Pass;
        this.formData.Enterprise_Info_SerialNumber = $.cookie('enterprise')
        return true
      }
      return false
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid && _this.check()) {
          _this.loading = true
          $.ajax({
            headers,
            url: BASE_URL + '/Flow/addApprova?token='+$.cookie('token'),
            type: 'POST',
            data: {
              json:JSON.stringify(_this.formData)
            },
            success: function (res) {
              if (res.status == 0) {
                sessionStorage.removeItem("approvalJSON");
                _this.$message.success(res.msg)
                window.history.go(-1);
              } else {
                _this.$message.error("操作失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})